package com.nexgo.payment.trans.setting.terminalkey.downkey;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.ui.BaseActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 母POS导密钥
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月30日
 */
public class DownloadKeyActivity extends BaseActivity implements DownloadKeyContract.View, BaseActivity.DownTimerListener {

    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;

    private DownloadKeyContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_download_key);
        ButterKnife.bind(this);

        initTitle();

        new DownloadKeyPresenter(this, Repository.getInstance());
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(R.string.import_masker_key))
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) mPresenter.start();
    }

    @OnClick(R.id.title_iv_left)
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void setPresenter(DownloadKeyContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.destroy();
        }
    }

    @Override
    public void showResult(String message) {
        if (!TextUtils.isEmpty(message)) {
            ToastUtils.showToast(message);
        }
        finish();
    }

    @Override
    public void showDownCount(int timeMills) {
        if (timeMills != 0){
            titleTvRight.setVisibility(View.VISIBLE);
            setDownTimer(timeMills, this);
        }
    }

    @Override
    public void onTick(long secondsUntilFinished) {
        titleTvRight.setText(String.valueOf(secondsUntilFinished));
    }

    @Override
    public void onFinish() {
        titleTvRight.setText(String.valueOf(0));
    }
}
